package br.edu.ufcg.fpc.sequential;

import java.io.File;

import br.edu.ufcg.fpc.util.Util;

/**
 * Executa o codigo de maneira sequencial.
 */
public class SequentialCode {

	public void increment() {
		File file = new File("increment.txt");
		Util.writeToFile(file, null);

		for (int i = 0; i < 1024; i++) {
			String content = Util.readFromFile(file);
			if (content == null || content.trim().isEmpty()) {
				content = String.format("Thread %d: %d", Thread.currentThread()
						.getId(), 1);
			} else {
				String[] auxList = content.split("\n");
				int length = auxList.length;
				String lastLine = auxList[length - 1];
				long newValue = Long.parseLong(lastLine.split(":")[1].trim()) + 1;
				content += String.format("\nThread %d: %d", Thread
						.currentThread().getId(), newValue);
			}
			Util.writeToFile(file, content);
		}
	}

}
